﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GrewalCRM.Entities;
using GrewalCRM.BLL;
using GrewalCRM.Entities.Common;

public partial class UC_SearchCritetia : System.Web.UI.UserControl
{
  CallLog_BLL callLogBll = new CallLog_BLL();
  List<CallLog> calls = null;
  List<ReportInstallationSummary> rptInstallarionSummary = null;
  CommonUtil common = new CommonUtil();

  public delegate void searchHandler(int? Month, int? Status, string zipCode);
  public event searchHandler Search;

  public delegate void CallSearchHandler(List<CallLog> Calls);
  public event CallSearchHandler CallSearch;

  public delegate void GetInstallationSummaryCashTransationReportMonthlyHandler(List<ReportInstallationSummary> rptInstallarionSummary);
  public event GetInstallationSummaryCashTransationReportMonthlyHandler GetMonthlyCashTransactionReport;

  #region Events
  protected void Page_Load(object sender, EventArgs e)
  {
      //Page.Form.DefaultButton = this.btnSearch.UniqueID;
    if (!Page.IsPostBack)
    {
      PopulateStatus();
      PopulateInatallers();
      PopulateDealer();
      PopulateACBrands();
      PopulateMonth();
      PopulateCounter();
      PopulateDay();
      setDefault();


    
        
    }

    ddlMonth.Enabled = rdMonthYear.Checked;
    //reqMonth.Enabled = rdMonthYear.Checked;
    txtYear.Enabled = rdMonthYear.Checked;
    //txtYear.ISRequired = rdMonthYear.Checked;

    ddlDay.Enabled = rdDay.Checked;

    dtFromDate.Enabled = rdDateRange.Checked;
    //dtFromDate.ISRequired = rdDateRange.Checked;
    dtToDate.Enabled = rdDateRange.Checked;
    //dtToDate.ISRequired = rdDateRange.Checked;
    
  }  
  public void btnSearch_Click(object sender, EventArgs e)
  {
    if (Search != null)
    {
      Search(Month, Status, txtZipCode.Text);
    }

    SearchCriteria criteria = new SearchCriteria();
    criteria.CallNo = CallNo;
    criteria.FromDate = FromDate;
    criteria.ToDate = ToDate;    
    criteria.CallTypeID = CallType;
    criteria.DealerCounterName = DealerCounterName;
    criteria.CounterName = CounterName;
    criteria.CallStatusID = Status;
    criteria.InstallerID = Installer;
    criteria.CustomerName = txtCustomerName.Text;
    criteria.Address = txtAddress.Text;
    criteria.ZipCode = txtZipCode.Text;
    criteria.ContactNo = txtContactNo.Text;
    criteria.ACBrandID= ACBrand;
    criteria.ACModelNo = txtACModel.Text;
    criteria.ACSerialNo = txtACSerialNo.Text;
    criteria.IsStrictSearch = chkIsStrict.Checked;
    criteria.IsTempClosed = chkIsTempClosed.Checked;    

    calls = callLogBll.GetAllCallsByAdvanceSearch(criteria);
    if (FetchMonthlyCashTransactionReport)
    {
      rptInstallarionSummary = callLogBll.GetInstallationSummaryCashTransationReportMonthly(FromDate, ToDate, CallType, DealerID, Status, Installer);

      if (GetMonthlyCashTransactionReport != null)
        GetMonthlyCashTransactionReport(rptInstallarionSummary);
    }

   
    if (CallSearch != null)
    {
      CallSearch(calls);
    }
  }
  #endregion

  #region Private Methods
  private void PopulateStatus()
  {
    ddlStatus.DataSource = MasterCRM.CallStatusCache;
    ddlStatus.DataTextField = "StatusName";
    ddlStatus.DataValueField = "ID";
    ddlStatus.DataBind();
    CommonUtil.AddDefaultItemToDDL(ddlStatus,"Call Status");
  }
  private void PopulateDealer()
  {
    ddlDealer.DataSource = MasterCRM.DealerCache;
    ddlDealer.DataTextField = "DealerCounterName";
    ddlDealer.DataValueField = "ID";
    ddlDealer.DataBind();
    CommonUtil.AddDefaultItemToDDL(ddlDealer, "Dealer");

  }        
  private void PopulateInatallers()
  {
    ddlInstaller.DataSource = MasterCRM.InstallersCache; ;
    ddlInstaller.DataTextField = "InstallerName";
    ddlInstaller.DataValueField = "ID";
    ddlInstaller.DataBind();
    CommonUtil.AddDefaultItemToDDL(ddlInstaller,"Installer");
  }
  private void PopulateACBrands()
  {
    ddlACBrands.DataSource = MasterCRM.ACBrandsCache;
    ddlACBrands.DataTextField = "ACBrandName";
    ddlACBrands.DataValueField = "ID";
    ddlACBrands.DataBind();
    CommonUtil.AddDefaultItemToDDL(ddlACBrands, "ACBrand");
  }
  private void PopulateMonth()
  {
    ddlMonth.DataSource = CommonUtil.GetEnumForBind( typeof(Month));
    ddlMonth.DataTextField = "value";
    ddlMonth.DataValueField = "key";
    ddlMonth.DataBind();
    CommonUtil.AddDefaultItemToDDL(ddlMonth, "Month");
  }
  private void PopulateCounter()
  {
    ddlCounter.DataSource = MasterCRM.DealerCache.Select(e => e.Counter).Distinct();   
    ddlCounter.DataBind();
    CommonUtil.AddDefaultItemToDDL(ddlCounter, "Counter");
  }

  private void PopulateDay()
  {
      ddlDay.Items.Add(new ListItem("Today", DateTime.Today.ToShortDateString()));
      ddlDay.Items.Add(new ListItem("Yesterday", DateTime.Today.AddDays(-1).ToShortDateString()));
      CommonUtil.AddDefaultItemToDDL(ddlDay, "Day");
  }

  private void setDefault()
  {
      ddlMonth.SelectedValue = DateTime.Today.Month.ToString();
      txtYear.Text = DateTime.Today.Year.ToString();

      ddlDay.SelectedValue = DateTime.Today.ToShortDateString();

      dtToDate.Date = DateTime.Today;
  }
  private void Bind()
  {
     
  }
  private ImasterCRM MasterCRM
  {
    get { return this.Page.Master as ImasterCRM; }
  }
  #endregion

  #region Public Properties
  public int? CallNo
  {
    get 
    {
      if (txtCallID.Text.Trim() == string.Empty)
        return null;
      else
        return int.Parse(txtCallID.Text);
    }   
  }
  public int? Month
  {
    get
    {
      if (ddlMonth.SelectedItem.Value == "0")
        return null;
      else
        return int.Parse(ddlMonth.SelectedItem.Value);
    }
  }
  private int? DealerID
  {
    get
    {
      if (ddlDealer.SelectedItem.Value == "0")
        return null;
      else
        return int.Parse(ddlDealer.SelectedItem.Value);
    }
  }
  private string DealerCounterName
  {
    get
    {
      if (ddlDealer.SelectedItem.Value == "0")
        return null;
      else
        return ddlDealer.SelectedItem.Text;
    }
  }
  private string CounterName
  {
    get
    {
      if (ddlCounter.SelectedItem.Value == "0")
        return null;
      else
        return ddlCounter.SelectedItem.Text;
    }
  }
  private int? CallType
  {
    get
    {
      if (ddlCallType.SelectedItem.Value == "0")
        return null;
      else
        return int.Parse(ddlCallType.SelectedItem.Value);
    }
  }
  public int? Status
  {
    get 
    { 
      if (ddlStatus.SelectedItem.Value == "0" )
        return null;
      else
        return int.Parse(ddlStatus.SelectedItem.Value);
    }      
  }
  public int? Installer
  {
    get
    {
      if (ddlInstaller.SelectedItem.Value == "0")
        return null;
      else
        return int.Parse(ddlInstaller.SelectedItem.Value);
    }
  }
  public int? ACBrand
  {
    get
    {
      if (ddlACBrands.SelectedItem.Value == "0")
        return null;
      else
        return int.Parse(ddlACBrands.SelectedItem.Value);
    }
  }
    private int? Year
    {
        get
        {
            int result = 0;
            if( ! int.TryParse(txtYear.Text.Trim(), out result))
                return null;
            return result;
        }
    }
    
  public DateTime? FromDate
  {
      get
      {
          DateTime fromDate = new DateTime();
          DateTime? date = null;
          string dateFormat = @"{0}/{1}/{2}";
          if (rdMonthYear.Checked)
          {              
              if (Month.HasValue && Year.HasValue)
              {
                  if (DateTime.TryParse(string.Format(dateFormat, Month.Value.ToString(), "1", Year.Value.ToString()), out fromDate))
                      date = fromDate;
              }          
          }
          else if (rdDay.Checked)
          {
              if (DateTime.TryParse(ddlDay.SelectedItem.Value, out fromDate))
                  date = fromDate;  
          }
          else if (rdDateRange.Checked)
          {
              date = dtFromDate.Date;      
          }

          return date;
      }
  }

  public DateTime? ToDate
  {
      get
      {
          DateTime toDate = new DateTime();
          DateTime? date = null;
          string dateFormat = @"{0}/{1}/{2}";
          int day = 0;
          if (rdMonthYear.Checked)
          {
              if (Month.HasValue && Year.HasValue)
              {

                  if (Year.Value % 4 == 0 && Month.Value == 2)
                      day = 29;
                  else if (Year.Value % 4 > 0 && Month.Value == 2)
                      day = 28;
                  else if (Month.Value == 1 || Month.Value == 3 || Month.Value == 5 || Month.Value == 7 || Month.Value == 8 || Month.Value == 10 || Month.Value == 12)
                      day = 31;
                  else
                      day = 30;

                  if (DateTime.TryParse(string.Format(dateFormat, Month.Value.ToString(), day.ToString(), Year.Value.ToString()), out toDate))
                      date = toDate;
              }                                    
          }
          else if (rdDay.Checked)
          {
              if (DateTime.TryParse(ddlDay.SelectedItem.Value, out toDate))
                  date = toDate;                   
          }
          else if (rdDateRange.Checked)
          {
              if (dtToDate.Date.HasValue)
                  date = dtToDate.Date.Value;
              else
                  return null;
          }

          return date;  
          
      }
  }

  public bool HideCallNo
  {
    get { return pnlCallID.Visible; }
    set { pnlCallID.Visible = !value; }
  }
  public bool HideCustomerName
  {
    get { return pnlCustomerName.Visible; }
    set { pnlCustomerName.Visible = !value; }
  }
  public bool HideAddress
  {
    get { return pnlAddress.Visible; }
    set { pnlAddress.Visible = !value; }
  }
  public bool HideZipCode
  {
    get { return pnlZipCode.Visible; }
    set { pnlZipCode.Visible = !value; }
  }
  public bool HideContactNo
  {
    get { return pnlContactNo.Visible; }
    set { pnlContactNo.Visible = !value; }
  }
  public bool HideACBrand
  {
    get { return pnlACBrand.Visible; }
    set { pnlACBrand.Visible = !value; }
  }
  public bool HideAcModelNo
  {
    get { return pnlACModel.Visible; }
    set { pnlACModel.Visible = !value; }
  }
  public bool HideACSerialNo
  {
    get { return pnlACSerialNo.Visible; }
    set { pnlACSerialNo.Visible = !value; }
  }
  public bool HideIsStrict
  {
    get { return chkIsStrict.Visible; }
    set { chkIsStrict.Visible = !value; }
  }
  public bool HideIsTempClosed
  {
    get { return chkIsTempClosed.Visible; }
    set { chkIsTempClosed.Visible = !value; }
  }

  public bool FetchMonthlyCashTransactionReport
  {
    get;
    set;
  }

  public bool IsDealerRequired
  {
    get { return reqDealer.Enabled; }
    set 
    {
      reqDealer.Enabled = value;
      reqDealer.Visible = value;
      lblDlrReq.Visible = value;
    }
  }

  public bool IsMonthRequired
  {
    get { return reqMonth.Enabled; }
    set
    {
      reqMonth.Enabled = value;
      reqMonth.Visible = value;
      lblMonthReq.Visible = value;
    }
  }

  #endregion
   
    
    
}